﻿using J.Model;
using J.ProductGet.Application.Command.ClassifiedManagementCommand;
using MediatR;
using ProductDto;
using SqlSugar;
using StatusDto;

namespace J.ProductGet.Application.CommandHandler.ClassifiedManagementCommandHandler
{
    public class ClassifiedManagementGetCommandHandler : IRequestHandler<ClassifiedManagementGetCommand, PageDto<ClassifiedManagement>>
    {
        private readonly ISqlSugarClient _sqlSugarClient;
        public ClassifiedManagementGetCommandHandler(ISqlSugarClient sqlSugarClient)
        {
            _sqlSugarClient = sqlSugarClient;
        }

        public async Task<PageDto<ClassifiedManagement>> Handle(ClassifiedManagementGetCommand request, CancellationToken cancellationToken)
        {
            var list = _sqlSugarClient.Queryable<ClassifiedManagement>();
            if (!string.IsNullOrEmpty(request.ClassificationName))
            {
                list = list.Where(s => s.ClassificationName.Contains(request.ClassificationName));
            }

            int PageTotal = list.Count();
            int PageCount = (int)Math.Ceiling(PageTotal * 1.0 / request.pageSize);


            var query = await list.Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToListAsync();
            return new PageDto<ClassifiedManagement>
            {
                Data = query,
                pagedCount = PageCount,
                pageIndex = request.pageIndex,
                pageSize = request.pageSize,
                totalCount = PageTotal
            };
        }
    }
}
